package com.qhg.saas_server.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.MyException.roadNotFindExcepiton;
import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.mapper.RoadMapper;
import com.qhg.saas_server.mapper.RoadStoreMapper;
import com.qhg.saas_server.pojo.RoadStore;
import com.qhg.saas_server.pojo.dto.RoadDto;
import com.qhg.saas_server.pojo.Road;
import com.qhg.saas_server.pojo.Store;
import com.qhg.saas_server.service.RoadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class RoadServiceImpl implements RoadService {

    @Autowired
    RoadMapper roadMapper;

    @Autowired
    RoadStoreMapper roadStoreMapper;

    /**
     * 条件查询路线列表
     * @param hashMap
     * @return
     */
    @Override
    public Result getRoadListByCondition(HashMap<String, Object> hashMap) {

        Integer pageSize = 10;

        Integer pageNum = 1;

        String orderType = "ASC";

        if(!Objects.isNull(hashMap)){

            if(hashMap.get("pageSize")!=null && hashMap.get("pageNum") != null){

                pageNum = (Integer) hashMap.get("pageNum");
                pageSize = (Integer) hashMap.get("pageSize");
            }

            //获取排序方式
            Integer sortMode = (Integer)hashMap.get("sortMode");

            if(!Objects.isNull(sortMode) && sortMode == 1){
                orderType = "DESC";
            }

        }

        //开启分页
        Page<Object> page = PageHelper.startPage(pageNum, pageSize, "road.roadCreateTime  " + orderType);

        List<Road> list = roadMapper.list(hashMap);

        PageInfo<Road> roadPageInfo = new PageInfo<>(list);
        roadPageInfo.setTotal(page.getTotal());

        return  Result.success(roadPageInfo);
    }

    /**
     * 获取路线的详细信息，根据roadId
     * @param roadId
     * @return
     * @throws roadNotFindExcepiton
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result getRoadDetailsByRoadId(Integer roadId) throws roadNotFindExcepiton {

        //获取路线信息
        Road road = roadMapper.getRoadById(roadId);
        //判断路线是否存在，不存在择抛出异常
        if (Objects.isNull(road)) throw new roadNotFindExcepiton("该路线不存在");

        //根据roadId,查询路线店面关联表，店面信息和排列序号信息
        List<Store> storeList = roadMapper.getRoadStoreListByRoadId(roadId);

        //将信息封装到roadDto中
        RoadDto roadDto = new RoadDto();
        BeanUtils.copyProperties(road,roadDto);
        roadDto.setStoreList(storeList);
        return Result.success(roadDto);

    }

    /**
     * 添加路线
     * @param roadDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addRoad(RoadDto roadDto) {

        //将roadDto中的信息copy到road中，用于存入路线表中
        Road road = new Road();
        BeanUtils.copyProperties(roadDto,road);
        roadMapper.insert(road);

        //插入roadStore表中将路线节点store存入
        Integer roadId = road.getRoadId();
        List<Store> storeList = roadDto.getStoreList();
        roadStoreMapper.insertByList(roadId,storeList);

        return Result.success("添加路线成功");
    }

    /**
     * 删除路线
     * @param roadId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteRoad(Integer roadId) {

        //删除road表中信息
        roadMapper.delete(roadId);
        //删除roadStore表中信息
        roadStoreMapper.delete(roadId);

        return Result.success("删除成功");
    }

    /**
     * 跟新路线
     * @param roadDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateRoad(RoadDto roadDto) {

        //跟新路线表
        Road road = new Road();
        BeanUtils.copyProperties(roadDto,road);
        roadMapper.update(road);

        //删除该路线下的所得roadStore信息
        Integer roadId = roadDto.getRoadId();
        roadStoreMapper.delete(roadId);

        //将跟新的信息重新插入到roadStore中
        List<Store> storeList = roadDto.getStoreList();
        roadStoreMapper.insertByList(roadId,storeList);

        return Result.success("跟新成功");

    }

    /**
     * 清空road中的路线信息(detils)
     * @param roadId
     */
    @Override
    public void clearAllRoadDetils(Integer roadId) {

        roadStoreMapper.delete(roadId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoadDetil(Integer roadId, Integer storeId) {

        //根据roadId,和storeId查询roadStore表中信息，获取序列号，用于更新
        RoadStore roadStore = roadStoreMapper.selectByRoadIdAndStoreId(roadId,storeId);
        Integer serial = roadStore.getSerial();

        //删除roadStroe中的单条记录
        roadStoreMapper.deleteRoadDetil(roadId,storeId);


        //跟新，删除roadStore中的单挑记录后，跟新序列号操作(使大于被删除的序列号减一) 根据roadId,和被删除的序列号

        roadStoreMapper.updateSerial(roadId,serial);
    }


}
